#! /bin/bash
COUNTRY="CN" #"AU" "AT" "BD" "BY" "BE" "BA" "BR" "BG" "CA" "CL" "CN" "CO" "HR" "CZ" "DK" "EC" "FI" "FR" "DE" "GR" "HK" "HU" "IS" "IN" "ID" "IR" "IE" "IL" "IT" "JP" "KZ" "LV" "LT" "LU" "MK" "MX" "AN" "NC" "NZ" "NO" "PH" "PL" "PT" "QA" "RO" "RU" "RS" "SG" "SK" "SI" "ZA" "KR" "ES" "SE" "CH" "TW" "TH" "TR" "UA" "GB" "US" "VN"
TIMEZONE_REGION="Asia"    #/etc/locale.time
TIMEZONE_CITY="Shanghai"  #/etc/locale.time
LOCALE_GEN="zh_CN.UTF-8 en_US.UTF-8" #/etc/locale.gen
LANG="en_US.UTF-8" #etc/locale.conf
HOSTNAME="zyk-PC" # /etc/hostname
TOPDIR=`pwd`

function color_echo()
{
    while (($#!=0))
    do
            case $1 in
                    -b)
                            echo -ne " ";
                    ;;
                    -t)
                            echo -ne "\t";
                    ;;
                    -n)     echo -ne "\n";
                    ;;
                    -black)
                            echo -ne "\033[30m";
                    ;;
                    -red)
                            echo -ne "\033[31m";
                    ;;
                    -green)
                            echo -ne "\033[32m";
                    ;;
                    -yellow)
                            echo -ne "\033[33m";
                    ;;
                    -blue)
                            echo -ne "\033[34m";
                    ;;
                    -purple)
                            echo -ne "\033[35m";
                    ;;
                    -cyan)
                            echo -ne "\033[36m";
                    ;;
                    -white|-gray) echo -ne "\033[37m";
                    ;;
                    -reset)
                            echo -ne "\033[0m";
                    ;;
                    -h|-help|--help)
                            echo "Usage: color_echo -color1 message1 -color2 message2 ...";
                            echo "eg:       color_echo -red [ -blue message1 message2 -red ]";
                    ;;
                    *)
                    echo -ne "$1"
                    ;;
            esac
            shift
    done
}

function get_repo_zyk()
{
    git clone https://e.coding.net/ykzhai/zyk/zyk.git  /home/zyk/.zyk
}

DiskPartionROOT=""
DiskPartionBOOT=""
DiskPartionSWAP=""
DiskPartionHOME=""

DeviceName=`sudo dmidecode  | grep -i "System Information"  -A 3 | grep -i Product |awk -F ": " '{print $2}' |sed 's/ /_/g'`
DeficeConfigsFile="$TOPDIR/localConf/$DeviceName"
if [ -f "$DeficeConfigsFile" ];then
    source "$DeficeConfigsFile"
else
    touch "$DeficeConfigsFile"
    color_echo -green "Please Edit configure file for this device: $DeviceName  "$DeficeConfigsFile"\n"
fi

function archlinux_0_DoSysDiskFormat()
{
    umount $DiskPartionBOOT
    umount $DiskPartionROOT
    swapoff  $DiskPartionSWAP
    umount  $DiskPartionSWAP

    color_echo -yellow "\n\n Warning: Will format Device: \n$DiskPartionBOOT\n$DiskPartionROOT\n$DiskPartionHOME\n$DiskPartionSWAP\nPlease input yes/no for confirm: \n" -reset -n
    read yes_no

    if [ $yes_no = "yes" ];then
        color_echo -red "mkfs.fat -F32 $DiskPartionBOOT"
        mkfs.fat -F32 $DiskPartionBOOT

        color_echo -red "mkfs.ext4     $DiskPartionROOT"
        mkfs.ext4     $DiskPartionROOT

        if [ "$DiskPartionHOME" != "" ];then
            umount $DiskPartionHOME
            color_echo -red "mkfs.ext4     $DiskPartionHOME"  # /HOME 一般不格式化
            mkfs.ext4     $DiskPartionHOME  # /HOME 一般不格式化
        fi
        color_echo -red "mkswap        $DiskPartionSWAP"
        mkswap        $DiskPartionSWAP
    fi
    color_echo -green
    df -h
}

function archlinux_0_DoSysDiskMount()
{
        echo "swapon $DiskPartionSWAP"
        swapon $DiskPartionSWAP
        mount $DiskPartionROOT /mnt
        mkdir  /mnt/boot/efi -p #EFI
        mount  $DiskPartionBOOT /mnt/boot/efi
        if [ "$DiskPartionHOME" != "" ];then
            mkdir  /mnt/home -p
            mount  $DiskPartionHOME /mnt/home
        fi
        color_echo -green
        df -h
}

function archlinux_1_DoBootStrapArchChRoot()
{
    pacman -Syy
    pacstrap /mnt base  base-devel linux linux-firmware linux-headers  zsh dhcpcd iwd htop vim git git-crypt reflector dmidecode dosfstools grub efibootmgr
    genfstab -U -p /mnt > /mnt/etc/fstab
    color_echo -green "Dump fstab \n"
    cat /mnt/etc/fstab
    if [ ! -d "/mnt/root/InstallArchLinux" ];then
        color_echo -green "cp  /root/InstallArchLinux /mnt/root -a"
        cp /root/InstallArchLinux /mnt/root -a
    fi
    arch-chroot /mnt /bin/zsh
}

function archlinux_2_DoBasicCoreConfig()
{
    ln -sf /usr/share/zoneinfo/$TIMEZONE_REGION/$TIMEZONE_CITY /etc/localtime
    hwclock --systohc --utc
    for locale in `echo $LOCALE_GEN`;do
        color_echo -green $locale
        sed -i "s/"#""$locale"/$locale/g" /etc/locale.gen
    done
    locale-gen
    localectl set-locale LANG=$LANG

    # Install efi grub config
    if (mount | grep efivarfs > /dev/null 2>&1);then
        rm -f /sys/firmware/efi/efivars/dump-*
        grub-install --target=`uname -m`-efi --efi-directory=/boot/efi --bootloader-id=ArchLinux --recheck
        grub-mkconfig -o /boot/grub/grub.cfg
    else
        color_echo -red  "ERROR: install_grub Support Only UEFI Config"
    fi
}

function archlinux_3_DoConfigUser()
{
    NEWUSER="zyk"
    color_echo  -green "Please Input Root Admin Passwd:\n" -reset
    passwd
    useradd -m -G users -s /bin/zsh $NEWUSER
    color_echo -green "Please Input NewUser"" $NEWUSER""\'s""Passwd:" -n -reset
    passwd $NEWUSER
    #TODO:  Read Return passwd
    mv /root/ArchLinux /home/$NEWUSER/
    echo "$NEWUSER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
    get_repo_zyk()
}

function  archlinux_4_DoPrepareReboot()
{
    sudo pacman -S plasma xorg ttf-dejavu wqy-microhei  xf86-input-synaptics plasma kde-applications networkmanager  --noconfirm
    sudo systemctl enable dhcpcd
    sudo systemctl enable NetworkManager sddm dhcpcd
}

